<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\admin\service\SmsService;
use app\admin\service\TeacherCountService;
use app\model\PackageOrder;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $html ="<p style='padding: 25px;font-size: 22px'>欢迎您的登录！</p>";
        echo $html;die();
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $search_year_arr = [
            'half_year' =>__('half_year'), // 6 months
            'one_year' => __('one_year'), // 12 months
            'two_years' => __('two_years'), // 24 months
            'three_years' => __('three_years'), // 36 months
        ];
        $search_year = input('search_year','one_year');
        $monthsArr = generateDateRange($search_year);
        $column = [];
        $starttime = Date::unixtime('day', -10);
        $endtime = Date::unixtime('day', 0, 'end');
       /* $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();*/

        $starttime_str = date("Y-m-d H:i:s",$starttime);
        $endtime_str = date("Y-m-d H:i:s",$endtime);
/*        $orderlist = Db("package_order")->where('create_time', 'between time', [$starttime_str, $endtime_str])
            ->field('create_time, status, COUNT(*) AS nums, DATE_FORMAT(create_time, "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();*/

        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }




        $yearFirst = date("Y")."-01-01";
        $totalorderMoney = PackageOrder::where('status',2)->where("create_time",'>',$yearFirst)->sum('price');


        $teacherService = new TeacherCountService();
        $teacherData = $teacherService->getAllTeacher();
        $this->view->assign([
            'totaluser'         => User::count(),
            'totaladmin'        => count($teacherData),
            'totalcategory'     => \app\common\model\Category::count(),
            'totalorder'        => PackageOrder::where('status',2)->where("create_time",'>',$yearFirst)->count(),
            'totalorderMoney'   => sprintf("%.2f",$totalorderMoney),

        ]);


        // 当前年份，你可以根据需要修改
       /* $year = input('year',date('Y'));
        $months = [];
        $month_dates = [];

        // 循环生成每个月的信息
        for ($month = 1; $month <= 12; $month++) {
            // 格式化月份名称
            $month_name = date('F', mktime(0, 0, 0, $month, 1, $year));
            $months[] = __($month_name);

            // 获取月份的第一天和最后一天
            $first_day = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
            $last_day = date('Y-m-t', mktime(0, 0, 0, $month, 1, $year));
            $month_dates[] = [
                'first_day' => $first_day,
                'last_day' => $last_day
            ];
        }*/
//        ppd($year,$month_dates);

        $userdata = $orderdata = [];
        foreach ($monthsArr as $kk=>$monthInfo){
            $monthInfoArr = explode('-',$monthInfo);
            $year = $monthInfoArr[0];
            $month = $monthInfoArr[1];
            $first_day = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
            $last_day = date('Y-m-t', mktime(0, 0, 0, $month, 1, $year));

            $s_time = $first_day;
            $e_time = $last_day;
            $s_time0 = strtotime($first_day);
            $e_time0 = strtotime($last_day);
            $whereTime = "createtime >= '{$s_time0}' and createtime <= '{$e_time0}'";
            $userData[$kk] = db('user')->where($whereTime)->count();
            $whereTime = "create_time >= '{$s_time}' and create_time <= '{$e_time}'";
            $orderdata[$kk] = db('package_order')->where('status',2)->where($whereTime)->sum('price');
        }
//        pp($userData);
        $this->assignconfig('column', $monthsArr);
        $this->assignconfig('userData', $userData);
        $this->assignconfig('orderData', $orderdata);


        $diffTime = time()+3600*24*3;
        $endTime = "end_time < {$diffTime}";
        $packageOrderModel = new PackageOrder();
        $packageOverOrder = db('package_order')->where([
                'is_over'=>-1,
                'is_finish'=>-1,
            ])->where($endTime)->select();
        foreach ($packageOverOrder as &$item){
            $userData = db('user')->find($item['user_id']);
            if(empty($userData)){
                $item['user']['username'] = '-';
                $item['user']['nickname_en'] = '-';
                $item['user']['bianhao'] = '-';
            }else{
                $item['user']['nickname_en'] = $userData['nickname_en'];
                $item['user']['username'] = $userData['username'];
                $item['user']['bianhao'] =  $userData['bianhao'];
            }

            if($item['type'] == 2){
                $item['time_num'] = __('Unlimited');
                $item['left_time_num'] = '-';
            }
            $item['begin_time'] = date("Y-m-d H:i",$item['begin_time']);
            $item['end_time'] = date("Y-m-d H:i",$item['end_time']);

            $packageData = db('package')->find($item['package_id']);
            $packageData = $this->formateDataBylang($packageData,['name'],false);
            $item['package_name'] = $packageData['name'];
        }
        $this->view->assign('packageOverOrder',$packageOverOrder);
        $this->view->assign('search_year_arr',$search_year_arr);
        $this->view->assign('current_year',$search_year);
        return $this->view->fetch();
    }

}
